package com.mtaite.study.mybatis.controller;

import com.mtaite.study.mybatis.flux.handler.UserHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@Configuration
public class UserRouter {

    @Autowired
    private UserHandler userHandler;

    @Bean
    public RouterFunction<ServerResponse> routerFunction() {
        return route(GET("/flux/user/list").and(accept(MediaType.APPLICATION_JSON)), userHandler::list)
                .andRoute(GET("/flux/user/find/{id}").and(accept(MediaType.APPLICATION_JSON)), userHandler::findById)
                .andRoute(POST("/flux/user/add").and(contentType(MediaType.APPLICATION_JSON)), userHandler::add)
                .andRoute(PUT("/flux/user/update").and(contentType(MediaType.APPLICATION_JSON)), userHandler::update)
                .andRoute(DELETE("/flux/user/delete/{id}"), userHandler::deleteId)
                .andRoute(DELETE("/flux/user/exdelete/{id}"), userHandler::deleteById);
    }
}